/**
 * Copyright &copy; 2012-2017 <a href="http://minglisoft.cn">HongHu</a> All rights reserved.
 */
package com.honghu.cloud.controller;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.honghu.cloud.bean.FTPServer;
import com.honghu.cloud.code.ResponseCode;
import com.honghu.cloud.dto.AccessoryDto;
import com.honghu.cloud.dto.RaffleDto;
import com.honghu.cloud.dto.SysConfigDto;
import com.honghu.cloud.dto.UserDto;
import com.honghu.cloud.feign.SysConfigFeignClient;
import com.honghu.cloud.service.IFTPServerService;
import com.honghu.cloud.tools.ftp.SFtpUtil;
import com.honghu.cloud.tools.qrcode.QRCodeUtil;
import com.honghu.cloud.utils.EncryptUtil;
import com.jcraft.jsch.ChannelSftp;

/**
 * 二维码controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/qrCode")
public class QrCodeController {

	@Autowired
	private SysConfigFeignClient sysConfigFeignClient;
	@Autowired
	private IFTPServerService ftpServerService;

	/**
	 * 生成用户分销二维码
	 * @explan
	 * @param congelationDto
	 * @return
	 */
	@RequestMapping(value = "/apiForFeign/myQrcode", method = RequestMethod.POST)
	public String myQrcode(@RequestBody UserDto user) {
		if(StringUtils.isNoneEmpty(user.getDirect_selling_qr_path())) {
			return user.getDirect_selling_qr_path();
		}

		// 获取用户头像，如果不存在取系统配置默认头像
		SysConfigDto sysConfigDto = sysConfigFeignClient.getSysConfig();
		AccessoryDto photo = user.getPhoto();
		if(photo == null) {
			photo = sysConfigDto.getQr_logo();
		}
		
		// 头像完整url
		String logoPath = sysConfigDto.getImageWebServer() + photo.getPath()
				+ File.separator + photo.getName();
		
		String uploadFilePath = sysConfigDto.getUploadFilePath();
		// 二维码目录
		String destPath = uploadFilePath + File.separator + "direct_selling_qr";
		// 二维码名称
		String qrName = UUID.randomUUID().toString() + "_qr.jpg";
		
        // FTP服务器配置
		List<FTPServer> ftpServers = ftpServerService.queryPageList(new HashMap<String, Object>());
		if (ftpServers == null || ftpServers.size() < 1) {
			return "";
		}
		
		// 上传文件到SFTP
		FTPServer ftpServer = ftpServers.get(0);
		ChannelSftp sftp = SFtpUtil.connect(ftpServer.getFtp_ip(), ftpServer.getFtp_port(), 
				ftpServer.getFtp_username(), EncryptUtil.decrypt(ftpServer.getFtp_password()));
		if (sftp == null) {
			return "";
		}
		
		// 将注册链接，生成二维码输入流
		String web_url = sysConfigDto.getH5Url();
        String content = web_url + "/" + "register" +"?directSellingParent_id="+user.getId();
        InputStream is = QRCodeUtil.encodeInput(content, logoPath, true);
        
        Boolean flag = SFtpUtil.upload(sftp, is, destPath, qrName);
		if (!flag) {
			return "";
		}
        
		return destPath +File.separator+ qrName;
	}

	/**
	 * 生成抽奖二维码
	 * @explan
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/apiForFeign/raffleQR", method = RequestMethod.POST)
	public String raffleQR(@RequestBody RaffleDto raffle) {
		if(StringUtils.isNoneEmpty(raffle.getRaffle_qr_path())) {
			return raffle.getRaffle_qr_path();
		}

		// 取系统配置默认头像
		SysConfigDto sysConfigDto = sysConfigFeignClient.getSysConfig();

		AccessoryDto photo = sysConfigDto.getQr_logo();

		// 完整url
		String logoPath = sysConfigDto.getImageWebServer() + photo.getPath()
				+ File.separator + photo.getName();

		String uploadFilePath = sysConfigDto.getUploadFilePath();
		// 二维码目录
		String destPath = uploadFilePath +  "/" + "raffle_qr";
		// 二维码名称
		String qrName = UUID.randomUUID().toString() + "_qr.jpg";

		// FTP服务器配置
		List<FTPServer> ftpServers = ftpServerService.queryPageList(new HashMap<String, Object>());
		if (ftpServers == null || ftpServers.size() < 1) {
			return "";
		}

		// 上传文件到SFTP
		FTPServer ftpServer = ftpServers.get(0);
		ChannelSftp sftp = SFtpUtil.connect(ftpServer.getFtp_ip(), ftpServer.getFtp_port(), 
				ftpServer.getFtp_username(), EncryptUtil.decrypt(ftpServer.getFtp_password()));
		if (sftp == null) {
			return "";
		}

		// 将注册链接，生成二维码输入流
		String web_url = sysConfigDto.getH5Url();
		String content = web_url + "/#/pages/home/index/live_draw?id="+raffle.getId();
		InputStream is = QRCodeUtil.encodeInput(content, null, true);

		Boolean flag = SFtpUtil.upload(sftp, is, destPath, qrName);
		if (!flag) {
			return "";
		}

		return destPath +"/"+ qrName;
	}
	
	/**
	 * 获取公众号二维码
	 * @return weixin_qr_img
	 */
	@RequestMapping(value = "/getQr", method = RequestMethod.POST)
	public Map<String,Object> getQr(){
		SysConfigDto sysConfig = sysConfigFeignClient.getSysConfig();
		if(null ==sysConfig){
			return ResponseCode.buildCodeMap("20001","内容不存在",null);
		}
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("obj", sysConfig.getWeixin_qr_img());
		return ResponseCode.buildSuccessMap(resultMap);
	}
}